package cn.tedu.ivos.vehicle.service.impl;

import cn.tedu.ivos.base.exception.ServiceException;
import cn.tedu.ivos.base.response.StatusCode;
import cn.tedu.ivos.vehicle.mapper.VehicleMapper;
import cn.tedu.ivos.vehicle.pojo.dto.VehicleParam;
import cn.tedu.ivos.vehicle.pojo.dto.VehicleQuery;
import cn.tedu.ivos.vehicle.pojo.entity.Vehicle;
import cn.tedu.ivos.vehicle.pojo.vo.VehicleVO;
import cn.tedu.ivos.vehicle.service.VehicleService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class VehicleServiceImpl implements VehicleService {
    @Autowired
    private VehicleMapper vehicleMapper;

    @Override
    public List<VehicleVO> selectvehicle(VehicleQuery vehicleQuery) {
        return vehicleMapper.selectvehicle(vehicleQuery);
    }

    @Override
    public void saveVehicle(VehicleParam vehicleParam) throws ParseException {
        Vehicle vehicle = new Vehicle();
        BeanUtils.copyProperties(vehicleParam,vehicle);
        if (vehicle.getId()==null){
        //     进行新增操作,先判断该用户名是否存在
            VehicleQuery vehicleQuery= new VehicleQuery();
            vehicleQuery.setLicense(vehicle.getLicense());
            vehicle.setCode(vehicle.getCode());
            if (vehicleMapper.selectvehicle(vehicleQuery)!=null){ //判断车牌号是否存在 不存在则新增数据
                vehicle.setCreateTime(new Date());
                vehicle.setGeofenceBindStatus("0");
                vehicle.setStatus("1");
                // 设置购买时间和上牌时间
                // SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                // vehicle.setBuyTime(sdf.parse(vehicleParam.getBuyTime()));
                // vehicle.setRegTime(sdf.parse(vehicleParam.getRegTime()));
                vehicleMapper.insertVehicle(vehicle);
            }else{
                // 请求的数据不存在
                throw new ServiceException(StatusCode.DATA_UNEXISTS);
            }
        }else { //进行更新操作
            vehicle.setUpdateTime(new Date());
            vehicleMapper.update(vehicle);
        }

    }

    @Override
    public void delete(Long id) {
        vehicleMapper.deleteById(id);
    }

    @Override
    public void unbindVehicle(Long vehicleId) {
        Vehicle vehicle=new Vehicle();
        vehicle.setId(vehicleId);
        vehicle.setGeofenceBindStatus("0");
        vehicle.setGeofenceId(null);
        vehicle.setUpdateTime(new Date());
    //     之前的update方法有 if test !=null,要求如果值为null则不更新
    //     但是现在业务更新的值就是null,需要给车辆接触绑定
    //     所以需要单独设置一个Mapper层的方法,给当前车辆的geofence_id设为null
        vehicleMapper.updateNullValue(vehicle);
    }

    @Override
    public void bindVehicle(Long geofenceId, Long vehicleId) {
        Vehicle vehicle=new Vehicle();
        vehicle.setId(vehicleId);
        vehicle.setGeofenceId(geofenceId);
        vehicle.setGeofenceBindStatus("1");
        vehicle.setUpdateTime(new Date());
        vehicleMapper.update(vehicle);
    }


}
